home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / scriptloader.js < prev    next >
Text File  |  2010-02-02  |  8KB  |  240 lines

  1. window.addEventListener('load', function(event) { WiseStampScriptLoader.init(event); }, false);
  2. window.addEventListener('unload', function(event) { WiseStampScriptLoader.uninit(event); }, false);
  3.  
  4. var WiseStampScriptLoader = 
  5. {
  6.     _prefs: null,
  7.  
  8.     get prefs() 
  9.     {
  10.         if (!this._prefs) 
  11.         {
  12.             this._prefs = Components.classes["@mozilla.org/preferences-service;1"].
  13.             getService(Components.interfaces.nsIPrefService);
  14.             this._prefs = this._prefs.getBranch("extensions.wisestamp.");
  15.         }
  16.         return this._prefs;
  17.     },
  18.  
  19.     scripts: 
  20.     {
  21.         gmail: 
  22.         {
  23.             file: "webmail/gmail.js",
  24.             /* normal and google app hosted */
  25.             regex: [/https?:\/\/mail\.google\.com\/(a\/)?.*/],
  26.             useParent: false
  27.         },
  28.  
  29.         yahooclassic: 
  30.         {
  31.             file: "webmail/yahooclassic.js",
  32.             regex: [/https?:\/\/.*mail\.yahoo\.com\/mc\/compose.*/],
  33.             useParent: false
  34.         },
  35.  
  36.         yahooclassic2: 
  37.         {
  38.             file: "webmail/yahooclassic2.js",
  39.             regex: [/https?:\/\/.*mail\.yahoo\.com\/mc\/welcome.*_pg=compose.*/],
  40.             useParent: true
  41.         },
  42.  
  43.         yahoo: 
  44.         {
  45.             file: "webmail/yahoo.js",
  46.             //regex: [/https?:\/\/.*mail\.yahoo\.com\/dc\/launch.*/],
  47.             //regex: [/https?:\/\/.*mail\.yahoo\.com\/dc\/blank.*/],
  48.             regex: [/https?:\/\/.*mail\.yahoo\.com.*/],
  49.             useParent: false
  50.         },
  51.  
  52.         aol: 
  53.         {
  54.             file: "webmail/aol.js",
  55.             regex: [/https?:\/\/webmail\.aol\.com\/.*\/aim\/.+\/s|Suite\.aspx/],
  56.             useParent: false
  57.         },
  58.  
  59.         hotmail: 
  60.         {
  61.             file: "webmail/hotmail.js",
  62.             regex: [/https?:\/\/.*mail\.live\.com\/mail\/.*/],
  63.             useParent: false
  64.         }
  65.     },
  66.  
  67.     init: function(event) 
  68.     {
  69.         var appcontent = window.document.getElementById("appcontent");
  70.         if (appcontent && !appcontent.wisestamped) 
  71.         {
  72.             appcontent.wisestamped = true;
  73.             appcontent.addEventListener("DOMContentLoaded", this, false);
  74.         }
  75.     },
  76.     
  77.     uninit: function(event) 
  78.     {
  79.         var appcontent = window.document.getElementById("appcontent");
  80.         if (appcontent) appcontent.removeEventListener("DOMContentLoaded", this, false);
  81.     },
  82.     
  83.     handleEvent: function(event) 
  84.     {
  85.         WiseStampUtils.log("[ScriptLoader.js::handleEvent] event.type = " + event.type);
  86.         
  87.         if((event.type=="load" || event.type=="DOMContentLoaded") && WisestampOverlay.enabled)
  88.         {
  89.             var unsafeWin = event.target.defaultView;
  90.             if (unsafeWin.wrappedJSObject) 
  91.                 unsafeWin = unsafeWin.wrappedJSObject;
  92.  
  93.               var eventWin = unsafeWin;
  94.  
  95.             var unsafeLoc = new XPCNativeWrapper(unsafeWin, "location").location;
  96.             var href = new XPCNativeWrapper(unsafeLoc, "href").href;
  97.             var usingParent = false;
  98.             if (href == "about:blank") 
  99.             {
  100.                 WiseStampUtils.log("[ScriptLoader.js::handleEvent] href == about:blank");
  101.                 usingParent = true;
  102.                 unsafeWin = event.target.defaultView.parent;
  103.  
  104.                 if (unsafeWin.wrappedJSObject) 
  105.                     unsafeWin = unsafeWin.wrappedJSObject;
  106.  
  107.                 unsafeLoc = new XPCNativeWrapper(unsafeWin, "location").location;
  108.                 href = new XPCNativeWrapper(unsafeLoc, "href").href;
  109.             }
  110.  
  111.             WiseStampUtils.log("[ScriptLoader.js::handleEvent] href = " + href);
  112.         
  113.             if (this.canLoad(href)) 
  114.             {
  115.                 WiseStampUtils.log("[ScriptLoader.js::handleEvent] canLoad = true");
  116.                 
  117.                 for (var site in this.scripts) 
  118.                 {
  119.                     var script = this.scripts[site];
  120.                     if (usingParent && script.useParent == false) // yahooclassic2 specific patch [Sasha]
  121.                         continue;
  122.  
  123.                     var match = script.regex.some(function(regex) { return regex.test(href); }, this);
  124.                     if (match) 
  125.                     {
  126.                         WiseStampUtils.log("[ScriptLoader.js::handleEvent] identified " + site + ", launching " + script.file + ", usingParent=" + usingParent);
  127.                         
  128.                         var wisestampsigjs=this.getUrlContents("chrome://wisestamp/content/webmail/wisestampsig.js");
  129.                         var sitejs = this.getUrlContents("chrome://wisestamp/content/" + script.file);
  130.                         var scriptjs = sitejs + wisestampsigjs;
  131.                         this.injectScript(site,scriptjs, href, unsafeWin, eventWin);
  132.                     } //else
  133.                     //WiseStampUtils.log("[WiseStampScriptLoader.js::handleEvent] No site identified.");
  134.                 }
  135.             } else
  136.                 WiseStampUtils.log("[ScriptLoader.js::handleEvent] canLoad = false");
  137.         
  138.         }
  139.     },
  140.     
  141.     injectScript: function(site,script, href, unsafeContentWin, eventWin) 
  142.     {
  143.         WiseStampUtils.log("[ScriptLoader.js::injectScript] >>>");
  144.         
  145.         var sandbox;
  146.         var safeWin = new XPCNativeWrapper(unsafeContentWin);
  147.  
  148.         sandbox = new Components.utils.Sandbox(safeWin);
  149.         sandbox.window = safeWin;
  150.         sandbox.document = sandbox.window.document;
  151.         sandbox.unsafeWindow = unsafeContentWin;
  152.         sandbox.eventWin = eventWin;
  153.  
  154.         // patch missing properties on xpcnw
  155.         sandbox.XPathResult = Components.interfaces.nsIDOMXPathResult;
  156.         sandbox.__proto__ = sandbox.window;
  157.  
  158.         sandbox.WiseStampLog = function(msg) { WiseStampUtils.log(msg); };
  159.         sandbox.strip_tags = function(str, allowed_tags) { return WiseStampUtils.strip_tags(str, allowed_tags); };
  160.         
  161.         sandbox.WiseStampSigCreated = function() 
  162.         { 
  163.             WiseStampComm.writeLog(WiseStampComm.LOG_EVENT_SIG_INSERT,site); 
  164.         }
  165.         
  166.         sandbox.HasExtension = function(id) 
  167.         {
  168.             var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
  169.             var addon = em.getItemForID(id);
  170.             return addon != null;
  171.         }
  172.         
  173.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].
  174.         getService(Components.interfaces.nsIPrefService);
  175.         prefs = prefs.getBranch("extensions.wisestamp.");
  176.         sandbox.WiseStampPrefs = WiseStampPrefs;
  177.         sandbox.GetValue = function(aPref) {
  178.             return prefs.getCharPref(aPref);
  179.         }
  180.         sandbox.GetBoolValue = function(aPref) {
  181.             return prefs.getBoolPref(aPref);
  182.         }
  183.         sandbox.ToDataURI = function(aURL) {
  184.             return WiseStampScriptLoader.toDataURI(aURL);
  185.         }
  186.         sandbox.GetSignature = function GetSignature_name(aType) {
  187.             return WisestampSignatureFactory.createSignature(null, aType);
  188.         }
  189.         try {    
  190.             WiseStampUtils.log("[ScriptLoader.js::injectScript] injecting...");
  191.             //WiseStampUtils.log(script);
  192.             
  193.             Components.utils.evalInSandbox("(function(){" + script + "})()", sandbox);
  194.             //WiseStampUtils.log("[ScriptLoader.js::injectScript] done injecting");
  195.         } catch(e) {
  196.             WiseStampUtils.log("[ScriptLoader.js::injectScript] exception caught - " + e);
  197.             Components.utils.reportError(e);
  198.         }
  199.     },
  200.     
  201.     canLoad: function(url) 
  202.     {
  203.         var scheme = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).extractScheme(url);
  204.         return ((scheme == "http" || scheme == "https" || scheme == "file") && !/hiddenWindow\.html$/.test(url));
  205.     },
  206.     
  207.     getUrlContents: function(aUrl) 
  208.     {
  209.         var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  210.         var scriptableStream = Components.classes["@mozilla.org/scriptableinputstream;1"].getService(Components.interfaces.nsIScriptableInputStream);
  211.  
  212.         var channel = ioService.newChannel(aUrl, null, null);
  213.         var input = channel.open();
  214.         scriptableStream.init(input);
  215.         var str = scriptableStream.read(input.available());
  216.         scriptableStream.close();
  217.         input.close();
  218.  
  219.         return str;
  220.     },
  221.     
  222.     toDataURI: function(aURL) 
  223.     {
  224.         var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  225.         var chan = ios.newChannel(aURL, null, null);
  226.         var file = null;
  227.         if (chan instanceof Components.interfaces.nsIFileChannel) 
  228.         {
  229.             file = chan.file;
  230.         }
  231.         
  232.         var contentType = Components.classes["@mozilla.org/mime;1"].getService(Components.interfaces.nsIMIMEService).getTypeFromFile(file);
  233.         var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
  234.         inputStream.init(file, 0x01, 0600, 0);
  235.         var stream = Components.classes["@mozilla.org/binaryinputstream;1"].createInstance(Components.interfaces.nsIBinaryInputStream);
  236.         stream.setInputStream(inputStream);
  237.         var encoded = btoa(stream.readBytes(stream.available()));
  238.         return "data:" + contentType + ";base64," + encoded;
  239.     }
  240. };